package Database;

import Entity.DurationDetails;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author CONG
 */
public class DurationDetailsDB {

    private ConnectDB conn;

    public DurationDetailsDB() {
        conn = new ConnectDB();
    }

    public ArrayList<DurationDetails> getDurationDetailsList(int accountCode) {
        ArrayList<DurationDetails> listDurDe = new ArrayList<DurationDetails>();
        try {
            CallableStatement cstmt = conn.connect().prepareCall("{call getDurationDetailsList(?)}");
            cstmt.setInt(1, accountCode);
            ResultSet rs = cstmt.executeQuery();
            while (rs.next()) {
                int accountLoanCode = rs.getInt("AccountLoanCode");
                int fineCode = rs.getInt("FineCode");
                Date dateDue = rs.getDate("DateDue");
                float amountDue = rs.getFloat("AmountDue");
                float amountPaid = rs.getFloat("AmountPaid");
                Date paidDate = rs.getDate("PaidDate");
                boolean status = rs.getBoolean("Status");
                DurationDetails durde = new DurationDetails(accountLoanCode, fineCode, dateDue, amountDue, amountPaid, paidDate, status);
                listDurDe.add(durde);
            }
        } catch (Exception ex) {
            Logger.getLogger(DurationDetailsDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listDurDe;
    }

    public void updateDurationHistory(DurationDetails durde) {
        try {
            CallableStatement cstmt = conn.connect().prepareCall("{call updateDurationHistory(?,?,?,?,?,?,?)}");
            cstmt.setInt("accountLoanCode", durde.getAccountLoanCode());
            cstmt.setInt("fineCode", durde.getFineCode());
            cstmt.setDate("dateDue", new java.sql.Date(durde.getDateDue().getTime()));
            cstmt.setFloat("amountDue", durde.getAmountDue());
            cstmt.setFloat("amountPaid", durde.getAmountPaid());
            cstmt.setDate("paidDate", new java.sql.Date(durde.getPaidDate().getTime()));
            cstmt.setBoolean("status", durde.isStatus());
//            System.out.println(durde.getAccountLoanCode() + "" + durde.getFineCode());
//            System.out.println(new java.sql.Date(durde.getDateDue().getTime()));
//            System.out.println(new java.sql.Date(durde.getPaidDate().getTime()));
            cstmt.executeUpdate();
        } catch (Exception ex) {
            Logger.getLogger(DurationDetailsDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void insertDurationDetails(DurationDetails durde) {
        try {
            SimpleDateFormat dateFormat=new SimpleDateFormat("MM/dd/yyyy");
            CallableStatement cstmt = conn.connect().prepareCall("{call insertDurationDetails(?,?,?,?,?,?)}");
            cstmt.setInt("accountLoanCode", durde.getAccountLoanCode());
            cstmt.setInt("fineCode", durde.getFineCode());
            cstmt.setDate("dateDue", new java.sql.Date(durde.getDateDue().getTime()));
            cstmt.setFloat("amountDue", durde.getAmountDue());
            cstmt.setFloat("amountPaid", durde.getAmountPaid());
            cstmt.setDate("paidDate", null);
            cstmt.executeUpdate();
        } catch (Exception ex) {
            Logger.getLogger(DurationDetailsDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
